<?php
/**
 * LICENSE
 * 
 * [license information]
 * 
 * @category   Nine
 * @copyright  Copyright (c) 2009 visualidea.org
 * @license    http://license.visualidea.org
 * @version    v 1.0 2009-04-15
 */
require_once 'Nine/Model.php';
class Models_Lecture extends Nine_Model
{ 
    /**
     * The primary key column or columns.
     * A compound key should be declared as an array.
     * You may declare a single-column primary key
     * as a string.
     *
     * @var mixed
     */
    protected $_primary = 'lecture_id';
    /**
     * The field name what we use to group all language rows together
     * 
     * @var string
     */
    
    protected $_multilingualFields = array();
    
    /**
     * Constructor.
     *
     * Supported params for $config are:
     * - db              = user-supplied instance of database connector,
     *                     or key name of registry instance.
     * - name            = table name.
     * - primary         = string or array of primary key(s).
     * - rowClass        = row class name.
     * - rowsetClass     = rowset class name.
     * - referenceMap    = array structure to declare relationship
     *                     to parent tables.
     * - dependentTables = array of child tables.
     * - metadataCache   = cache for information from adapter describeTable().
     *
     * @param  mixed $config Array of user-specified config options, or just the Db Adapter.
     * @return void
     */
    public function __construct($config = array())
    {
        $this->_name = $this->_prefix . 'lecture';
        return parent::__construct($config); 
    }

    /**
     * Get all product with conditions
     * 
     * @param $condition
     * @param $order
     * @param $count
     * @param $offset
     */
    public function getAllLectures($condition = array(), $order = null, $count = null, $offset = null)
    {
        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('c' => $this->_name))
                ->join(array('cc' => $this->_prefix . 'product_category'), 'c.subject_id = cc.product_category_gid', array('cname' => 'name'))
                ->order($order)
                ->limit($count, $offset);
        /**
         * Conditions
         */
        if (null != @$condition['keyword']) {
            $sql = "SELECT lecture_id FROM {$this->_name} WHERE " . $this->getAdapter()->quoteInto('title LIKE ?', "%{$condition['keyword']}%");
            $ids = $this->_db->fetchAll($sql);
            if (empty($ids)) {
                return array();
            }
            $idString = '';
            foreach ($ids as $row) {
                $idString .= $row['lecture_id'] . ',';
            }
            /**
             * Add to select object
             */
            $select->where('c.lecture_id IN (' . trim($idString, ',') .')');
        }
        if (null != @$condition['subject_id']) {
        	
        	
            /**
             * Add to select object
             */
            $select->where('c.subject_id IN (' . @$condition['subject_id'] .')');
        }
        
        return $this->fetchAll($select)->toArray();
    }
    
     public function getLectureById( $gid ) 
    {
    	$select = $this->select()
    			->where('lecture_id=?',$gid);
    	
    	return @reset($this->fetchAll($select)->toArray());
    }
    
    public function getLectureBy_Id( $gid, $lang_id ) 
    {
    	$select = $this->select()
    			->where('lecture_gid=?',$gid)
                ->where('lang_id=?',$lang_id);
    	
    	return @reset($this->fetchAll($select)->toArray());
    }
    
    public function getLectureByCatId( $gid )
    {
    	$select = $this->select()
    	->where('subject_id=?',$gid);
    	 
    	return $this->fetchAll($select)->toArray();
    }
    
   
    
}